package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.repository.mapper.AnnouncementManagementMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.List;
import java.util.Map;

@Service
public class AnnouncementManagementService {
    @Resource
    private AnnouncementManagementMapper announcementManagementMapper ;

    @Value("${Doc.uploadPath}")
    private String UP_ROOT_PATH;

    @Value("${Doc.filePath}")
    private String FILE_PATH;

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getAllNotice(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        List<Map<String, Object>> list = null;
        list = announcementManagementMapper.getAllNotice(model);
        for(int i=0;i<list.size();i++){
            String str = (String) list.get(i).get("boardLoad");
            if (!str.equals("")){
                String boardLoadName=str.substring(((String) list.get(i).get("boardLoad")).lastIndexOf("/")+1);
                list.get(i).put("boardLoadName",boardLoadName);
            }
        }
        PageHelper.clearPage();
        return list;
    }

    @Transactional(readOnly = true)
    public List<Map<String, Object>> getOnlyNotice(QueryModel model) {
        List<Map<String, Object>> list = null;
        list = announcementManagementMapper.getOnlyNotice(model);
        for(int i=0;i<list.size();i++){
            String str = (String) list.get(i).get("boardLoad");
            if (!str.equals("")){
                String boardLoadName=str.substring(((String) list.get(i).get("boardLoad")).lastIndexOf("/")+1);
                list.get(i).put("boardLoadName",boardLoadName);
            }
        }
        return list;
    }

    @Transactional
    public int addNewNotice(QueryModel model) {
        return announcementManagementMapper.addNewNotice(model);
    }

    @Transactional
    public int updateNotice(QueryModel model) {
        return announcementManagementMapper.updateNotice(model);
    }

    @Transactional
    public int publishNotice(List<Integer> list) {
        return announcementManagementMapper.publishNotice(list);
    }

    @Transactional
    public int deleteNotice(List<Integer> list) {
        return announcementManagementMapper.deleteNotice(list);
    }

    /**
     * 上传附件业务逻辑
     *
     * @param uploadFile
     */
    public String fileUpload(MultipartFile uploadFile) {
        try {
            InputStream in = uploadFile.getInputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            // 文件上传的路径
            String path =
                    UP_ROOT_PATH
                            + "/"
                            + uploadFile.getOriginalFilename();

            File file = new File(path);
            File fileParent = file.getParentFile();
            if (!fileParent.exists()) {
                fileParent.mkdirs();
            }
            OutputStream out = new FileOutputStream(path);
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.close();
            in.close();
            return FILE_PATH + "/" +uploadFile.getOriginalFilename();
        } catch (IOException e) {
            return "系统错误，请联系管理员";
        }
    }

}
